//
//  GYDProfileViewModel.swift
//  Weibo
//
//  Created by 贵永冬 on 16/7/16.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit
import SnapKit

class GYDProfileViewModel: NSObject, UIScrollViewDelegate {
    weak private var profileVC : GYDProfileViewController?
    
    private var headerGgView : UIImageView?
    
    
    private var tableView : UITableView?
    
    init(profileVC : GYDProfileViewController) {
        super.init()
        self.profileVC = profileVC
    }
    func startService() -> Void {
        let isLogin = GYDUserModel.sharedInstance.isLogin
        if isLogin {
            self.hiddenVisitorView()
            showProfileView()
        }else {
            self.showVisitorView()
        }
        GYDUserModel.sharedInstance.loginResultSignal.subscribeNext { [unowned self] (x) in
            if x as! String == LOGIN_SUCCESS {
                self.hiddenVisitorView()
                self.showProfileView()
            }else if x as! String == LOGINOUT_SUCCESS {
                self.showVisitorView()
            }
        }
    }
    private func showVisitorView() -> Void {
        let settingBarItem = UIBarButtonItem(title: "设置", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(GYDProfileViewModel.settingButtonClick))
        self.profileVC?.navigationItem.rightBarButtonItem = settingBarItem
        self.profileVC?.navigationItem.leftBarButtonItem = nil
        
        let subViews = self.profileVC?.view.subviews
        for view in subViews! {
            view.removeFromSuperview()
        }
        
        let scrollView = UIScrollView(frame: self.profileVC!.view.bounds)
        scrollView.delegate = self
        scrollView.tag = 900
        scrollView.contentSize = CGSize(width: GYDWindowWidth, height: scrollView.height - 64 - 48)
        scrollView.showsVerticalScrollIndicator = false
        self.profileVC?.view.addSubview(scrollView)
        
        let coverImageView = UIImageView(image: UIImage(named: "profile_cover_background"))
        coverImageView.frame = CGRect(x: 0, y: 0, width: GYDWindowWidth, height: 150)
        self.headerGgView = coverImageView
        scrollView.addSubview(coverImageView)
        
        let userImageView = UIImageView(image: UIImage(named: "radar_card_head_occupying"))
        coverImageView.addSubview(userImageView)
        userImageView.layer.cornerRadius = 35
        userImageView.layer.masksToBounds = true
        userImageView.snp_makeConstraints { (make) in
            make.bottom.equalTo(coverImageView.snp_bottom).offset(-40)
            make.width.equalTo(70)
            make.height.equalTo(70)
            make.centerX.equalTo(coverImageView.snp_centerX)
        }
        
        let userViewBtn = UIButton(type: UIButtonType.Custom)
        userViewBtn.frame = CGRect(x: 0, y: coverImageView.bottom, width: GYDWindowWidth, height: 40)
        userViewBtn.backgroundColor = UIColor.whiteColor()
        scrollView.addSubview(userViewBtn)
        
        let descLabel = UILabel(frame: CGRect(x: 8, y: 0, width: 150, height: userViewBtn.height))
        let attriButedText = NSMutableAttributedString(string: "关注 快看大家都在关注谁", attributes: [NSForegroundColorAttributeName:RGB(150, g: 150, b: 150),NSFontAttributeName:UIFont.systemFontOfSize(11)])
        attriButedText.addAttributes([NSForegroundColorAttributeName:RGB(40, g: 40, b: 40),NSFontAttributeName:UIFont.systemFontOfSize(16)], range: NSMakeRange(0, 2))
        descLabel.attributedText = attriButedText
        userViewBtn.addSubview(descLabel)
        
        let goImageView = UIImageView(image: UIImage(named: "common_icon_small_arrow"))
        goImageView.frame = CGRect(x: userViewBtn.width - 15, y: (userViewBtn.height-10)/2.0, width: 10, height: 10)
        userViewBtn.addSubview(goImageView)
        

        let visitorView = GYDVisitorView(frame: CGRect(x: 0,y: userViewBtn.bottom, width: GYDWindowWidth, height:GYDWindowHeight - 64 - 49 - 190), visitorType: VisitorType.Profile)
        scrollView.addSubview(visitorView)
        visitorView.registSignal.subscribeNext { [unowned self] (x) in
            let loginVC = GYDLoginViewController()
            self.profileVC?.presentViewController(loginVC, animated: true, completion: nil)
        }
        visitorView.loginSignal.subscribeNext { [unowned self] (x) in
            let loginVC = GYDLoginViewController()
            self.profileVC?.presentViewController(loginVC, animated: true, completion: nil)
        }
    }
    
    @objc private func settingButtonClick() {
        let settingVC = GYDSettingViewController(style: UITableViewStyle.Grouped)
        self.profileVC?.navigationController?.pushViewController(settingVC, animated: true)
    }
    private func hiddenVisitorView() -> Void {
        let subViews = self.profileVC?.view.subviews
        for view in subViews! {
            view.removeFromSuperview()
        }
    }
    
    /**
     显示我的界面
     */
    private func showProfileView() {
        showLoginNavigationBar()
        
    }
    
    
    private func showLoginNavigationBar() {
        self.profileVC?.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "添加好友", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
        let settingBarItem = UIBarButtonItem(title: "设置", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(GYDProfileViewModel.settingButtonClick))
        self.profileVC?.navigationItem.rightBarButtonItem = settingBarItem
    }
    

    //MARK: - UIScrollViewDelegate
    func scrollViewDidScroll(scrollView: UIScrollView) {
        if scrollView.tag == 900 {
            let scrollY = scrollView.contentOffset.y
            if scrollY < -64 {
                self.headerGgView?.top = scrollY + 64
                self.headerGgView?.height = 150 - 64 - scrollY
            }else {
                self.headerGgView?.top = 0
                self.headerGgView?.height = 150
            }
        }
    }
}





